package com.yingyi.user.controller;

import com.yingyi.user.domain.Wxuser;
import com.yingyi.user.service.IWxuserService;
import com.yingyi.user.query.WxuserQuery;

import com.yingyi.basic.util.JsonResult;
import com.yingyi.basic.util.PageList;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
/**
 * (Wxuser)表控制层
 *
 * @author Yingyi
 * @since 2022-07-31 19:39:44
 */
@Api(value = "(Wxuser)")
@RestController
@RequestMapping("/wxuser")
public class WxuserController {
    /**
     * 服务对象
     */
    @Autowired
    private IWxuserService wxuserService;
    
    /**
     * 分页和条件查询
     * @param   query 分页查询和条件查询参数
     *
     * @return 查询结果
     */
    @ApiOperation(value = "分页查询和条件查询" )
    @ApiImplicitParams({
       @ApiImplicitParam(name="query", value="查询的参数",required = true,
           paramType = "query", dataType = "com.yingyi.user.query.WxuserQuery")
       })
    @PutMapping
    public PageList<Wxuser> pageQuery(@RequestBody WxuserQuery query){
        try {
                return wxuserService.queryData(query);
            } catch (Exception e) {
                e.printStackTrace();
                return new PageList<Wxuser>();
            }
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "通过主键查询")
    @ApiImplicitParams({
          @ApiImplicitParam(name="id", value="主键值", required = true, paramType = "path", dataType = "java.lang.Long")
          })
    @GetMapping("{id}")
    public Wxuser queryById(@PathVariable("id") Long id) {
        try {
            return wxuserService.queryById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    

    /**
     * 通过主键查询单条数据
     * @return 单条数据
     */
    @ApiOperation(value = "查询所有数据" )
    @GetMapping
    public List<Wxuser> queryAll(){
        try {
            return wxuserService.queryAll();
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<Wxuser>();
        }
    }
    

    /**
     * 增加或修改
     * @RequestBody 从请求体中获取数据
     */
    @ApiOperation(value = "增加或修改数据" )
    @ApiImplicitParams({
       @ApiImplicitParam(name="Wxuser wxuser",  value="待操作数据",required = true,
          paramType = "query", dataType = "com.yingyi.user.domain.Wxuser")
    })
    @PostMapping
    public JsonResult save(@RequestBody Wxuser wxuser){
        try {
            if(wxuser.getId()==null){
                //添加操作
                wxuserService.insert(wxuser);
            }else{
                //修改操作
                wxuserService.update(wxuser);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMessage("操作失败");
        }
    }


    /**
     * 删除数据
     *
     * @param id 主键
     * @return JsonResult
     */
    @ApiOperation(value = "通过主键删除数据" )
    @ApiImplicitParams({
        @ApiImplicitParam(name="id", value="主键值",required = true, paramType = "path",
            dataType = "java.lang.Long")
        })
    @DeleteMapping("/{id}")
    public JsonResult deleteById(@PathVariable Long id) {
        try {
            wxuserService.delete(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMessage("删除失败");
        }
    }
    
    
    /**
     * 批量删除
     * @param ids 待删除的数据的id
     * @return JsonResult
     */
    @ApiOperation(value = "批量删除")
    @ApiImplicitParams({
       @ApiImplicitParam(name="ids", value="待删除的ID集合",required = true, paramType = "query",
          dataType = "java.util.ArrayList")
    })
    @PatchMapping
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
            wxuserService.patchDelete(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMessage("删除失败，请稍后重试！");
        }
    }

}

